package com.pro.service.imple;

import com.pro.dao.DeptMapper;
import com.pro.entity.DeptPlus;
import com.pro.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: DeptServiceImple
 * @Author: 笑笑
 * @DATE: 2021/11/9
 * @TIME: 上午 10:35
 */
@Service
public class DeptServiceImple implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    //查询所有部门信息
    @Override
    public List<DeptPlus> getAllDept(DeptPlus deptPlus) {
        return deptMapper.getAllDept(deptPlus);
    }

    //根据id查询部门信息
    @Override
    public DeptPlus getById(Integer id) throws Exception {
        if (id == null || id < 1){
            throw new Exception("id不正确");
        }
        return deptMapper.getById(id);
    }

    //根据id修改部门信息
    @Override
    public Boolean updateDept(DeptPlus deptPlus) throws Exception {
        if (deptPlus.getId() < 1){
            throw new Exception("id不正确");
        }
        deptPlus.setUpdateDate(new Date());
        return deptMapper.updateDept(deptPlus);
    }

    //根据id删除部门信息
    @Override
    public Boolean deleteDept(Integer id) throws Exception {
        if (id == null || id < 1){
            throw new Exception("id不正确");
        }
        return deptMapper.deleteDept(id);
    }

    //添加部门信息
    @Override
    public Boolean addDept(DeptPlus deptPlus) {
        deptPlus.setCreateDate(String.valueOf(new Date()));
        return deptMapper.addDept(deptPlus);
    }

    //查询所有部门信息——树
    @Override
    public Map<String, Object> getDeptZtree() {
        List<DeptPlus> allDept = deptMapper.getAllDept(new DeptPlus());
        Map<String, Object> map = new HashMap<>();
        map.put("deptName", "所有部门");
        map.put("open", "true");
        map.put("children", allDept);
        return map;
    }

    //根据部门名称查询id
    @Override
    public Integer getNameById(String deptName) {
        return deptMapper.getNameById(deptName);
    }
}
